﻿--[[
   @brief    animation utility 
   @author   hqzheng
   @version  1.0
   @date     2016-11-24
--]]

animation_utility = {
    animation_parent_id = 0;
    -- 动画id
    move_animation_ids = {};
    size_animation_ids = {};
    ANI_TYPE = {
       ANI_MOVE = 0,
       ANI_SIZE = 1,
    };

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[animation_utility] %s", msg), core_msg);
    end;

    -- 初始化
    -- 参数:
    -- component_id：动画对象控件ID
    -- animation_relative：动画子布局（在AnimationUtility.tpf中配置）
    -- animation_type:动画类型
    -- return:nil
    init = function (component_id, animation_relative, animation_type)
        animation_utility.inner_log("[init]");
        local cur_move_animation_id = animation_utility.move_animation_ids[component_id];
        local cur_size_animation_id = animation_utility.size_animation_ids[component_id];
        if 0 == animation_utility.animation_parent_id then
            animation_utility.animation_parent_id = layout_mgr.restore_layout("AnimationUtility.tpf");
        end

        if animation_utility.ANI_TYPE.ANI_MOVE == animation_type then
            if nil == cur_move_animation_id or 0 == cur_move_animation_id then
                cur_move_animation_id = ui_utility.get_ctrl_id_by_path(
                    animation_utility.animation_parent_id, animation_relative);
                animation_utility.move_animation_ids[component_id] = cur_move_animation_id;
            end
        elseif animation_utility.ANI_TYPE.ANI_SIZE == animation_type then
            if nil == cur_size_animation_id or 0 == cur_size_animation_id then
                cur_size_animation_id = ui_utility.get_ctrl_id_by_path(
                    animation_utility.animation_parent_id, animation_relative);
                animation_utility.size_animation_ids[component_id] = cur_size_animation_id;
            end
        end
    end;

    -- 移动动画
    -- 参数:
    -- component_id：动画对象控件的ID
    -- start_pos：开始位置
    -- end_pos：结束位置
    -- frames：动画帧
    -- deley：延迟开始时间
    -- return: nil
    move_animation = function (component_id, start_pos, end_pos, frames, deley)
        if nil == component_id or 0 == component_id then
            return;
        end

        local cur_animation_id = animation_utility.move_animation_ids[component_id];
        if nil == cur_animation_id or 0 == cur_animation_id then
            return;
        end

        animation_utility.inner_log(
            string.format("[move_animation]start_pos,x:%d,y:%d;end_pos,x:%d,y:%d;frames:%d",
            start_pos.x, start_pos.y, end_pos.x, end_pos.y, frames));

        local animation_ptr = ui_utility.get_ctrl_ptr_by_id(cur_animation_id);
        local component_ptr = ui_utility.get_ctrl_ptr_by_id(component_id);

        if nil ~= animation_ptr and nil ~= component_ptr then
            animation_ptr:SetBindObj(component_ptr);
            animation_ptr:put_frames(frames);
            animation_ptr:put_startPos(start_pos);
            animation_ptr:put_endPos(end_pos);
            if nil == deley then
                animation_ptr:put_delay(0);
            else 
                animation_ptr:put_delay(tonumber(deley));
            end
            animation_ptr:Start();
        end
    end;

    -- 缩放动画
    -- 参数:
    -- component_id：动画对象控件的ID
    -- start_size：开始尺寸
    -- end_size：结束尺寸
    -- frames：动画帧
    -- deley：延迟开始时间
    -- return: nil
    size_animation = function (component_id, start_size, end_size, frames, deley)
        if nil == component_id or 0 == component_id then
            return;
        end

        local cur_animation_id = animation_utility.size_animation_ids[component_id];
        if nil == cur_animation_id or 0 == cur_animation_id then
            return;
        end

        animation_utility.inner_log(
            string.format(
            "[size_animation]start_size,cx:%d,cy:%d;end_size,cx:%d,cy:%d;frames:%d",
            start_size.cx, start_size.cy, end_size.cx, end_size.cy, frames));

        local animation_ptr = ui_utility.get_ctrl_ptr_by_id(cur_animation_id);
        local component_ptr = ui_utility.get_ctrl_ptr_by_id(component_id);


        if nil ~= animation_ptr and nil ~= component_ptr then
            animation_ptr:SetBindObj(component_ptr);
            animation_ptr:put_frames(frames);
            animation_ptr:put_startSize(start_size);
            animation_ptr:put_endSize(end_size);
            if nil == deley then
                animation_ptr:put_delay(0);
            else
                animation_ptr:put_delay(deley);
            end
            animation_ptr:Start();
        end
    end;
};